package com.momo.conroller;

import com.momo.res.JSONResult;
import com.momo.service.WxMenuService;
import com.momo.utils.CheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Kagome on 2019/5/15.
 */
@Controller
@RequestMapping("/wx")
@Slf4j
public class WXInitialization {

    @Autowired
    private WxMenuService wxMenuService;
    @RequestMapping("/initialization/v1")
    public void initializationWX1(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String signature = req.getParameter("signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = req.getParameter("echostr");
       log.info("signature:{} timestamp:{} nonce:{} echostr:{}",signature,timestamp,nonce,echostr);
        //将response打印输出
        PrintWriter out = resp.getWriter();
        try {
            if (CheckUtil.checkSignature(signature,timestamp,nonce)){
                out.println(echostr);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("menuCreate")
    @ResponseBody
    public JSONResult wxMenuService() throws Exception{
        JSONResult jsonResult= wxMenuService.menuCreate();
       return jsonResult;
    }
}
